﻿
Partial Class WebForm_AddHonorCert
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.fv.InsertItemTemplate = Me.fv.ItemTemplate
        Me.fv.EditItemTemplate = Me.fv.ItemTemplate
    End Sub

    Protected Sub fv_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewCommandEventArgs) Handles FV.ItemCommand
        If e.CommandName = "btnSave" Then
            'Me.Visible = False
            RaiseEvent Saving(Me, New EventArgs)  

        End If
    End Sub
End Class

Partial Class WebForm_AddHonorCert
    Public Event Saving(ByVal sender As Object, ByVal e As EventArgs)

    Public Sub InitDataForEdit(ByVal honorCertID As String)
        Me.HonorCertID = honorCertID

        Me.DataAccessMode = My.DataAccessMode.Update
        Me.fv.ChangeMode(FormViewMode.Edit)
        Me.Visible = True
        Dim sql As New MyStringBuilder
        Dim dat As New clsData
        Dim dt As Data.DataTable
        sql.AppendFormat("select a.VolunteerName,b.*,'' as WorkHour from HonorCert b ")
        sql.AppendFormat("inner join VolunteerDistinctView a on a.VolunteerID=b.VolunteerID ")
        sql.AppendFormat("where HonorCertID = {0} ", Me.HonorCertID)
        dt = dat.GetData(sql)
        Me.fv.DataSource = dt
        Me.fv.DataBind()

        TryCast(Me.fv.FindControl("lblWorkHour"), Label).Visible = False
        TryCast(Me.fv.FindControl("txtWorkHour"), TextBox).Visible = False
        Me.BindDDLType()
        MyObj.MatchItem(TryCast(Me.fv.FindControl("ddlType"), DropDownList), dt.Rows(0)("Type").ToString())

    End Sub
    Public Sub BindDDLType()
        Dim ddlItem As DropDownList = TryCast(Me.fv.FindControl("ddlType"), DropDownList)
        ddlItem.DataTextField = "Text"
        ddlItem.DataValueField = "Value"

        ddlItem.DataSource = MyObj.GetCodeMappingData("HonorCert", "Type")
        ddlItem.DataBind()
        ddlItem.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())
    End Sub
    Public Sub InitDataForInsert(ByVal applicationID As String)
        Me.ApplicationID = applicationID

        Me.DataAccessMode = My.DataAccessMode.Insert
        Me.Visible = True
        Me.fv.ChangeMode(FormViewMode.Insert)

        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        sql.AppendFormat("select a.VolunteerName,b.* from HonorCertApplication b ")
        sql.AppendFormat("inner join VolunteerDistinctView a on a.VolunteerID=b.VolunteerID ")
        sql.AppendFormat("where ApplicationID = {0} ", applicationID)

        'MyObj.WriteLog(sql.ToString())

        Dim dt As Data.DataTable = dat.GetData(sql)
        TryCast(Me.fv.FindControl("lblWorkHour"), Label).Visible = False
        TryCast(Me.fv.FindControl("txtWorkHour"), TextBox).Visible = False
        TryCast(Me.fv.FindControl("txtHonorCertCode"), TextBox).Text = ""
        TryCast(Me.fv.FindControl("txtComment"), TextBox).Text = ""
        TryCast(Me.fv.FindControl("lblVolunteerNameText"), Label).Text = dt.Rows(0)("VolunteerName")
        Me.fv.Data("VolunteerID") = dt.Rows(0)("VolunteerID")
        Me.BindDDLType()
    End Sub
    Public Sub InsertWorkHour(ByVal volunteerID As String, ByVal volunteerName As String)
        Me.DataAccessMode = My.DataAccessMode.Insert

        Me.fv.ChangeMode(FormViewMode.Insert)
        TryCast(Me.fv.FindControl("lblWorkHour"), Label).Visible = True
        TryCast(Me.fv.FindControl("txtWorkHour"), TextBox).Visible = True
        TryCast(Me.fv.FindControl("txtWorkHour"), TextBox).Text = ""
        TryCast(Me.fv.FindControl("txtHonorCertCode"), TextBox).Text = ""
        TryCast(Me.fv.FindControl("lblVolunteerNameText"), Label).Text = volunteerName
        Me.fv.Data("VolunteerID") = volunteerID
        Me.BindDDLType()
    End Sub
    Public ReadOnly Property WorkHour() As String
        Get
            Return TryCast(Me.fv.FindControl("txtWorkHour"), TextBox).Text
        End Get
    End Property
    Public ReadOnly Property VolunteerID() As String
        Get
            Return Me.fv.Data("VolunteerID")
        End Get
    End Property

    Public Property HonorCertID() As String
        Get
            Return ViewState("HonorCertID")
        End Get
        Set(ByVal value As String)
            ViewState("HonorCertID") = value
        End Set
    End Property

    Public Property HonorCertCode() As String
        Get
            Return TryCast(Me.fv.FindControl("txtHonorCertCode"), TextBox).Text
        End Get
        Set(ByVal value As String)
            TryCast(Me.fv.FindControl("txtHonorCertCode"), TextBox).Text = value
        End Set
    End Property

    Public ReadOnly Property EndDate() As Date
        Get
            Return Me.fv.FindCalendarTxtControl("txtEndDate").Value
        End Get
    End Property

    Public Property ApplicationID() As String
        Get
            Return ViewState("ApplicationID")
        End Get
        Set(ByVal value As String)
            ViewState("ApplicationID") = value
        End Set
    End Property

    Public ReadOnly Property IssueDate() As DateTime
        Get
            'Return DateTime.Parse(TryCast(Me.fv.FindControl("txtIssueDate"), TextBox).Text)
            Return TryCast(Me.fv.FindControl("txtIssueDate"), WebForm_CommonControl_CalendarTextBox).Value
        End Get
    End Property

    Public Property DataAccessMode() As My.DataAccessMode
        Get
            If ViewState("DataAccessMode") Is Nothing Then
                Return My.DataAccessMode.Insert
            End If

            Return ViewState("DataAccessMode")
        End Get
        Set(ByVal value As My.DataAccessMode)
            ViewState("DataAccessMode") = value
        End Set
    End Property
    Public ReadOnly Property ApplyDate() As DateTime
        Get
            Return TryCast(Me.fv.FindControl("txtApplyDate"), WebForm_CommonControl_CalendarTextBox).Value
        End Get
    End Property
    Public Property Comment() As String
        Get
            Return TryCast(Me.fv.FindControl("txtComment"), TextBox).Text
        End Get
        Set(ByVal value As String)
            TryCast(Me.fv.FindControl("txtComment"), TextBox).Text = value
        End Set
    End Property
    Public Property Type() As String
        Get
            If TryCast(Me.fv.FindControl("ddlType"), DropDownList).SelectedValue Is Nothing Then
                TryCast(Me.fv.FindControl("ddlType"), DropDownList).SelectedValue = ""
            End If
            Return TryCast(Me.fv.FindControl("ddlType"), DropDownList).SelectedValue
        End Get
        Set(ByVal value As String)
            TryCast(Me.fv.FindControl("ddlType"), DropDownList).SelectedValue = value
        End Set
    End Property

End Class